<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <div ref="searchRef">
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="110px"
      >
        <el-form-item label="班组编码" prop="teamCode">
          <el-input
            v-model="queryParams.teamCode"
            placeholder="请输入班组编码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-220px"
          />
        </el-form-item>
        <el-form-item label="班组名称" prop="teamName">
          <el-input
            v-model="queryParams.teamName"
            placeholder="请输入班组名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-220px"
          />
        </el-form-item>
        <el-form-item label="所属部门" prop="configOrgDeptArchivesName">
          <el-input
            v-model="queryParams.configOrgDeptArchivesName"
            placeholder="请输入所属部门"
            clearable
            @keyup.enter="handleQuery"
            class="!w-220px"
          />
        </el-form-item>
        <el-form-item label="班组状态" prop="dictEnableStatus">
          <el-select
            v-model="queryParams.dictEnableStatus"
            placeholder="请选择班组状态"
            clearable
            class="!w-220px"
          >
            <el-option
              v-for="dict in getIntDictOptions('dict_enable_status')"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="btnWrap">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['config:org-team-archives:export']"
        >
          数据导出
        </el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['config:org-team-archives:create']"
        >
          新增
        </el-button>
      </div>
      <div>
        <el-button
          type="primary"
          @click="openTableTitleSet"
        >
          表头设置
        </el-button>
      </div>
    </div>
    <!-- 表单 -->
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      @register="register"
      border
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm('update', row.id)"
          v-hasPermi="['config:org-team-archives:update']"
        >
          编辑
        </el-button>
        <el-button
          link
          type="danger"
          @click="handleDelete(row.id)"
          :loading="deleteLoading"
          v-hasPermi="['config:org-team-archives:delete']"
        >
          删除
        </el-button>
      </template>
    </Table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrgTeamArchivesForm ref="formRef" @success="getList" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getTableTitle" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
</template>

<script setup lang="ts" name="OrgTeamArchives">
import { getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as OrgTeamArchivesApi from '@/api/config/orgTeamArchives'
import OrgTeamArchivesForm from './OrgTeamArchivesForm.vue'
import type { CrudSchema } from '@/hooks/web/useCrudSchemas'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const queryParams = reactive({
  pageNo: 1,
  teamCode: null,
  teamName: null,
  configOrgDeptArchivesName: undefined,
  dictEnableStatus: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const deleteLoading = ref(false)

const searchRef = ref() //查询条件ref

const tableTitleList = ref<CrudSchema[]>([]) // 表头列表
const code = 'OrgTeamArchives'  //表头名
const tableHeight = ref(0)

// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
const { tableObject, tableMethods } = useTable({
  getListApi: OrgTeamArchivesApi.getOrgTeamArchivesPage, // 分页接口
  defaultParams: queryParams
})

const parentRef = ref()
// 获取table组件
const register = (parent: any) => {
  parentRef.value = parent
}

// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

// 获取表头
const getTableTitle = async () => {
  const res = await OrgTeamArchivesApi.getOrgTeamArchivesPage(queryParams)
  res.labels.push({
    field: 'action',
    label: '操作',
    align: 'left',
    headerAlign: 'center',
    width: 110
  })
  const { allSchemas } = useCrudSchemas(res.labels)
  tableTitleList.value = allSchemas.tableColumns
}

// 打开表头设置
const tableTitleSet = ref()
const openTableTitleSet = () => {
  tableTitleSet.value.open(tableTitleList.value, code)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  setSearchParams(queryParams)
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  deleteLoading.value = true
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrgTeamArchivesApi.deleteOrgTeamArchives(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } finally {
    deleteLoading.value = false
  }
}

/** 导出按钮操作 */
const exportRef = ref()
const handleExport = async () => {
  exportRef.value.open()
}

// 下载
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: []
  }
  if (val.range === 'all') {
    params.ids = []
  } else {
    params.ids = parentRef.value.selections.map((item) => {
      return item['id']
    })
  }
  const data = await OrgTeamArchivesApi.exportOrgTeamArchives(params)
  download.excel(data, val.name)
  parentRef.value.clearTableSelect()
}

/** 初始化 **/
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
  tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 115
  await getTableTitle()
  getList()
})
</script>
<style lang="scss" scoped>
.btnWrap {
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
}

:deep(.el-select .el-input) {
  width: 100%;
}
</style>
